#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/9/19 19:23
# @Author  : Junzhao
# @File    : 学生管理系统.py
# @Software: PyCharm
"""未加入数据合法性检验"""
import sys
from typing import Dict, List

students: list[dict[str, str]] = []  # 储存学生信息的列表

# 测试用例
students.append({'id': "1", "name": "A", "sex": "male"})
students.append({'id': "2", "name": "B", "sex": "female"})
students.append({'id': "3", "name": "C", "sex": "female"})
students.append({'id': "4", "name": "D", "sex": "male"})
print(students)


def add():
    """"""
    student = {'id': input("请输入学号："),
               "name": input("请输入姓名："),
               "sex": input("请输入性别")}
    for i in students:
        if i.get("id") == student.get("id"):
            print("此学号已录入，请检查！")
            add()
        else:
            continue
    students.append(student)
    print("添加学生成功")
    pass


def modify():
    """"""
    id = input("请输入要修改的学生学号：")
    for i in students:
        if i.get('id') == id:
            print(f"学号:{i['id']} 姓名:{i['name']}")
            i['name'] = input("请输入修改后的名字")
            print("修改成功")
            return
    print("未找到此学生。")
    pass


def delete():
    """"""
    id = input("请输入要删除的学生学号：")
    for i in students:
        if i.get('id') == id:
            students.remove(i)
            print("删除成功")
            return
    print("未找到此学生。")
    pass


def check_by_id():
    id = input("请输入学号：")
    for i in students:
        if i.get('id') == id:
            print(f"学号:{i['id']} 姓名:{i['name']}")
            return
    print("未找到此学生。")
    pass


def check_by_name():
    flag = True
    # 用来打印同名学生
    name = input("请输入姓名：")
    for i in students:
        if i.get('name') == name:
            print(f"学号:{i['id']} 姓名:{i['name']}")
            if flag:
                flag = False
    if flag:
        print("未找到此学生。")
    pass


def show():
    """"""
    for student in students:
        print(f"学号:{student['id']} 姓名:{student['name']} 性别：{student['sex']}")
    pass


def menu():
    """"""
    print("=====================================")
    print("\t欢 迎 使 用 学 生 管 理 系 统 ！")
    print("\t                             v2.0")
    print("请选择功能(输入数字)")
    print("1 · 添加学生信息")
    print("2 · 删除学生信息")
    print("3 · 修改学生信息")
    print("4 · 查询学生信息")
    print("5 · 查询所有学生信息")
    print("0 · 退出系统")
    print("=====================================")

    select = input()
    if select == "1":
        add()
    elif select == "0":
        esc()
    elif select == "2":
        delete()
    elif select == "3":
        modify()
    elif select == "4":
        select = input("请选择根据什么信息查询\n "
                       "1 id(学号)\t"
                       "2 name(姓名)\n")
        if select == "1":
            check_by_id()
        elif select == "2":
            check_by_name()
        else:
            print("抱歉目前不支持此功能，或请检查您的输入！")
    elif select == "5":
        show()
    else:
        print("抱歉目前不支持此功能，或请检查您的输入！")
    menu()
    pass


def esc():
    """调用系统的函数退出程序"""
    print("感谢您的使用！")
    sys.exit(0)

    pass


if __name__ == "__main__":
    menu()
